/*
 * Copyright (C) 2016 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See http://www.gnu.org/licenses/gpl-2.0.html for more details.
 */

#include "mtk_cpufreq_config.h"
#include "mtk_cpufreq_platform.h"

#define NR_FREQ		16
#define ARRAY_COL_SIZE	4

static unsigned int FY_6785Tbl[NR_FREQ * NR_MT_CPU_DVFS][ARRAY_COL_SIZE] = {
	/* Freq, Vproc, post_div, clk_div */
	/* LL */
	{ 2000, 101, 1, 1 },
	{ 1933, 97, 1, 1 },
	{ 1866, 93, 1, 1 },
	{ 1800, 88, 1, 1 },
	{ 1733, 84, 1, 1 },
	{ 1666, 80, 2, 1 },
	{ 1618, 76, 2, 1 },
	{ 1500, 73, 2, 1 },
	{ 1375, 68, 2, 1 },
	{ 1275, 64, 2, 1 },
	{ 1175, 60, 2, 1 },
	{ 1075, 56, 2, 1 },
	{ 975, 52, 2, 1 },
	{ 875, 48, 2, 1 },
	{ 774, 44, 4, 1 },
	{ 500, 32, 4, 1 },



	/* L */
	{ 2002, 112, 1, 1 },
	{ 1955, 109, 1, 1 },
	{ 1907, 105, 1, 1 },
	{ 1860, 102, 1, 1 },
	{ 1796, 98, 1, 1 },
	{ 1733, 93, 1, 1 },
	{ 1670, 89, 1, 1 },
	{ 1530, 82, 2, 1 },
	{ 1419, 76, 2, 1 },
	{ 1308, 71, 2, 1 },
	{ 1169, 64, 2, 1 },
	{ 1085, 60, 2, 1 },
	{ 1002, 55, 2, 1 },
	{ 919, 51, 2, 1 },
	{ 835, 47, 2, 1 },
	{ 774, 44, 4, 1 },



	/* CCI */
	{ 1400, 101, 2, 1 },
	{ 1330, 95, 2, 1 },
	{ 1260, 88, 2, 1 },
	{ 1190, 81, 2, 1 },
	{ 1155, 78, 2, 1 },
	{ 1120, 74, 2, 1 },
	{ 1050, 71, 2, 1 },
	{ 980, 67, 2, 1 },
	{ 927, 64, 2, 1 },
	{ 875, 61, 2, 1 },
	{ 822, 59, 2, 1 },
	{ 752, 55, 2, 1 },
	{ 682, 51, 4, 1 },
	{ 612, 47, 4, 1 },
	{ 560, 44, 4, 1 },
	{ 500, 32, 4, 1 },

};

static unsigned int FY_6785TTbl[NR_FREQ * NR_MT_CPU_DVFS][ARRAY_COL_SIZE] = {
	/* Freq, Vproc, post_div, clk_div */
	/* LL */
	{ 2000, 101, 1, 1 },
	{ 1933, 97, 1, 1 },
	{ 1866, 93, 1, 1 },
	{ 1800, 88, 1, 1 },
	{ 1733, 84, 1, 1 },
	{ 1666, 80, 2, 1 },
	{ 1618, 76, 2, 1 },
	{ 1500, 73, 2, 1 },
	{ 1375, 68, 2, 1 },
	{ 1275, 64, 2, 1 },
	{ 1175, 60, 2, 1 },
	{ 1075, 56, 2, 1 },
	{ 975, 52, 2, 1 },
	{ 875, 48, 2, 1 },
	{ 774, 44, 4, 1 },
	{ 500, 32, 4, 1 },



	/* L */
	{ 2050, 115, 1, 1 },
	{ 1986, 111, 1, 1 },
	{ 1923, 106, 1, 1 },
	{ 1860, 102, 1, 1 },
	{ 1796, 98, 1, 1 },
	{ 1733, 93, 1, 1 },
	{ 1670, 89, 1, 1 },
	{ 1530, 82, 2, 1 },
	{ 1419, 76, 2, 1 },
	{ 1308, 71, 2, 1 },
	{ 1169, 64, 2, 1 },
	{ 1085, 60, 2, 1 },
	{ 1002, 55, 2, 1 },
	{ 919, 51, 2, 1 },
	{ 835, 47, 2, 1 },
	{ 774, 44, 4, 1 },



	/* CCI */
	{ 1400, 101, 2, 1 },
	{ 1330, 95, 2, 1 },
	{ 1260, 88, 2, 1 },
	{ 1190, 81, 2, 1 },
	{ 1155, 78, 2, 1 },
	{ 1120, 74, 2, 1 },
	{ 1050, 71, 2, 1 },
	{ 980, 67, 2, 1 },
	{ 927, 64, 2, 1 },
	{ 875, 61, 2, 1 },
	{ 822, 59, 2, 1 },
	{ 752, 55, 2, 1 },
	{ 682, 51, 4, 1 },
	{ 612, 47, 4, 1 },
	{ 560, 44, 4, 1 },
	{ 500, 32, 4, 1 },


};

static unsigned int FY_6783Tbl[NR_FREQ * NR_MT_CPU_DVFS][ARRAY_COL_SIZE] = {
	/* Freq, Vproc, post_div, clk_div */
	/* LL */
	{ 1800, 88, 1, 1 },
	{ 1766, 86, 1, 1 },
	{ 1733, 84, 1, 1 },
	{ 1700, 82, 2, 1 },
	{ 1666, 80, 2, 1 },
	{ 1618, 76, 2, 1 },
	{ 1525, 74, 2, 1 },
	{ 1450, 71, 2, 1 },
	{ 1375, 68, 2, 1 },
	{ 1275, 64, 2, 1 },
	{ 1175, 60, 2, 1 },
	{ 1075, 56, 2, 1 },
	{ 975, 52, 2, 1 },
	{ 875, 48, 2, 1 },
	{ 774, 44, 4, 1 },
	{ 500, 32, 4, 1 },



	/* L */
	{ 1800, 98, 1, 1 },
	{ 1765, 96, 1, 1 },
	{ 1733, 93, 1, 1 },
	{ 1701, 91, 1, 1 },
	{ 1670, 89, 1, 1 },
	{ 1530, 82, 2, 1 },
	{ 1475, 79, 2, 1 },
	{ 1419, 76, 2, 1 },
	{ 1364, 74, 2, 1 },
	{ 1308, 71, 2, 1 },
	{ 1169, 64, 2, 1 },
	{ 1085, 60, 2, 1 },
	{ 1002, 55, 2, 1 },
	{ 919, 51, 2, 1 },
	{ 835, 47, 2, 1 },
	{ 774, 44, 4, 1 },



	/* CCI */
	{ 1260, 88, 2, 1 },
	{ 1190, 81, 2, 1 },
	{ 1155, 78, 2, 1 },
	{ 1120, 74, 2, 1 },
	{ 1085, 73, 2, 1 },
	{ 1032, 70, 2, 1 },
	{ 980, 67, 2, 1 },
	{ 927, 64, 2, 1 },
	{ 875, 61, 2, 1 },
	{ 822, 59, 2, 1 },
	{ 770, 56, 2, 1 },
	{ 717, 53, 4, 1 },
	{ 665, 50, 4, 1 },
	{ 612, 47, 4, 1 },
	{ 560, 44, 4, 1 },
	{ 500, 32, 4, 1 },


};

unsigned int *xrecordTbl[NUM_CPU_LEVEL] = {
	[CPU_LEVEL_0] = &FY_6785Tbl[0][0],
	[CPU_LEVEL_1] = &FY_6785TTbl[0][0],
	[CPU_LEVEL_2] = &FY_6783Tbl[0][0],
};

#ifdef CCI_MAP_TBL_SUPPORT
static unsigned char CCI_6785Tbl[32][16] = {
	/* Normal CCI Map */
	{ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7},
	{ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7},
	{ 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8},
	{ 7, 7, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
	{ 7, 7, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
	{ 7, 7, 8, 8, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
	{ 7, 7, 8, 8, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
	{ 7, 7, 8, 8, 9, 9, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11},
	{ 7, 7, 8, 8, 9, 9, 10, 11, 12, 12, 12, 12, 12, 12, 12, 12},
	{ 7, 7, 8, 8, 9, 9, 10, 11, 12, 12, 13, 13, 13, 13, 13, 13},
	{ 7, 7, 8, 8, 9, 9, 10, 11, 12, 12, 13, 13, 13, 13, 13, 13},
	{ 7, 7, 8, 8, 9, 9, 10, 11, 12, 12, 14, 14, 14, 14, 14, 14},
	{ 7, 7, 8, 8, 9, 9, 10, 11, 12, 12, 14, 14, 15, 15, 15, 15},
	{ 7, 7, 8, 8, 9, 9, 10, 11, 12, 12, 14, 14, 15, 15, 15, 15},
	{ 7, 7, 8, 8, 9, 9, 10, 11, 12, 12, 14, 14, 15, 15, 15, 15},
	{ 7,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 7, 7, 7},
	{ 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8},
	{ 6, 7, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
	{ 6, 7, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
	{ 6, 7, 7, 8, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
	{ 6, 7, 7, 8, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
	{ 6, 7, 7, 8, 9, 9, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11},
	{ 6, 7, 7, 8, 9, 9, 10, 11, 12, 12, 12, 12, 12, 12, 12, 12},
	{ 6, 7, 7, 8, 9, 9, 10, 11, 12, 12, 13, 13, 13, 13, 13, 13},
	{ 6, 7, 7, 8, 9, 9, 10, 11, 12, 12, 13, 13, 13, 13, 13, 13},
	{ 6, 7, 7, 8, 9, 9, 10, 11, 12, 12, 14, 14, 14, 14, 14, 14},
	{ 6, 7, 7, 8, 9, 9, 10, 11, 12, 12, 14, 14, 15, 15, 15, 15},
	{ 6, 7, 7, 8, 9, 9, 10, 11, 12, 12, 14, 14, 15, 15, 15, 15},
	{ 6, 7, 7, 8, 9, 9, 10, 11, 12, 12, 14, 14, 15, 15, 15, 15},
	{ 6, 7, 7, 8, 9, 9, 10, 11, 12, 12, 14, 14, 15, 15, 15, 15},

	/* Perf CCI Map */
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{ 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
	{ 0, 0, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	{ 0, 0, 1, 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 9, 10, 10, 10, 10, 10},
	{ 0, 0, 1, 1, 2, 4, 5, 6, 6, 7, 9, 10, 11, 12, 12, 12},
	{ 0, 0, 1, 1, 2, 4, 5, 6, 6, 7, 9, 10, 11, 12, 13, 13},
	{ 0, 0, 1, 1, 2, 4, 5, 6, 6, 7, 9, 10, 11, 12, 14, 14},
	{ 0, 0, 1, 1, 2, 4, 5, 6, 6, 7, 9, 10, 11, 12, 14, 14},

};

static unsigned char enratio_FYTbl[(NR_FREQ + 1) * IMAX_EN_RATIO_TBL_NUM] = {
	5, 22, 23, 25, 26, 28, 30, 0, 0,
	    0,  0,  0,  0,  0,  0, 0, 0,
	5, 22, 23, 25, 26, 28, 30, 0, 0,
	    0,  0,  0,  0,  0,  0, 0, 0,
};

unsigned char *xrecord_IMAX_Tbl[NUM_CPU_LEVEL] = {
	[CPU_LEVEL_0] = enratio_FYTbl,
	[CPU_LEVEL_1] = enratio_FYTbl,
	[CPU_LEVEL_2] = enratio_FYTbl,
};
unsigned char *xrecord_CCI_Tbl[NUM_CPU_LEVEL] = {
	[CPU_LEVEL_0] = &CCI_6785Tbl[0][0],
	[CPU_LEVEL_1] = &CCI_6785TTbl[0][0],
	[CPU_LEVEL_2] = &CCI_6783Tbl[0][0],
};
#endif
